//
//  CustomAlert.m
//  AlertTest
//
//  Created by TGBUS on 12-7-26.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "CustomAlert.h"
#import "AppDelegate.h"


@interface CustomAlert ()

-(void)btnAction:(id)sender;

@end


@implementation CustomAlert

@synthesize delegate;

- (id)initWithMessage:(NSString *)message
{
    self = [super initWithFrame:CGRectMake(24, -170, 272, 165)];
    if (self) {
        
//        [self setBackgroundColor:[UIColor colorWithRed:130/255.0 green:0/255.0 blue:0/255.0 alpha:0.7]];
        UIImageView *bgImgView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"alert_bg.png"]];
        [bgImgView setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        [self addSubview:bgImgView];
        [bgImgView release];
        
        UILabel *lblMessage=[[UILabel alloc] initWithFrame:CGRectMake(16, 20, 240, 60)];
        [lblMessage setBackgroundColor:[UIColor clearColor]];
        [lblMessage setTextColor:[UIColor whiteColor]];
        [lblMessage setTextAlignment:UITextAlignmentCenter];
        [lblMessage setNumberOfLines:0];
        [lblMessage setFont:[UIFont systemFontOfSize:17]];
        [lblMessage setFont:[UIFont boldSystemFontOfSize:17]];
        [lblMessage setText:message];
        [self addSubview:lblMessage];
        [lblMessage release];
        

        UIButton *btnOK=[UIButton buttonWithType:UIButtonTypeCustom];
        [btnOK setFrame:CGRectMake(20, 100, 106, 37)];
        [btnOK setTag:500];
        [btnOK setBackgroundImage:[UIImage imageNamed:@"btnYES.png"] forState:UIControlStateNormal];
        [btnOK setBackgroundImage:[UIImage imageNamed:@"btnYES_pressed.png"] forState:UIControlStateHighlighted];
        [btnOK addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnOK];
        
        UIButton *btnNO=[UIButton buttonWithType:UIButtonTypeCustom];
        [btnNO setFrame:CGRectMake(146, 100, 106, 37)];
        [btnNO setTag:501];
        [btnNO setBackgroundImage:[UIImage imageNamed:@"btnNO.png"] forState:UIControlStateNormal];
        [btnNO setBackgroundImage:[UIImage imageNamed:@"btnNO_pressed.png"] forState:UIControlStateHighlighted];
        [btnNO addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btnNO];
        
        UIButton *btnCancel=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        [self addSubview:btnCancel];
    }
    return self;
}

-(void)btnAction:(id)sender
{
    UIButton *btn=(UIButton *)sender;
    
    AppDelegate *appDelegate=[[UIApplication sharedApplication]delegate];
    UIView *overlayer=[appDelegate.window viewWithTag:10000];
    
    [UIView animateWithDuration:0.4 delay:0 options:UIViewAnimationCurveEaseInOut animations:^{
        [overlayer setAlpha:0.0];
    } completion:^(BOOL finished){
        [overlayer removeFromSuperview];
    }];
    
    [UIView animateWithDuration:0.1 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^{
        [self setFrame:CGRectMake(20, 140, 280, 170)];
    } completion:^(BOOL finished){
        [UIView animateWithDuration:0.4 delay:0.0 options:UIViewAnimationCurveEaseInOut animations:^{
            [self setFrame:CGRectMake(20, -170, 280, 170)];
        } completion:nil];
    }];
    int index=0;
    switch (btn.tag) {
        case 500://YES
            index=0;
            break;
        case 501://NO
            index=1;
            break;
        default:
            break;
    }
[delegate customAlertYES:[NSNumber numberWithInt:index]];
}

-(void)show
{
    AppDelegate *appDelegate=[[UIApplication sharedApplication]delegate];
    
    UIView *overLayer=[[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)] autorelease];
    [overLayer setBackgroundColor:[UIColor blackColor]];
    [overLayer setAlpha:0.0];
    [overLayer setTag:10000];
    [appDelegate.window addSubview:overLayer];
    
    [appDelegate.window addSubview:self];
    
    [UIView animateWithDuration:0.4
                          delay:0.0
                        options:UIViewAnimationCurveEaseInOut
                     animations:^{
                         [self setFrame:CGRectMake(20, 140, 280, 170)];
                         [overLayer setAlpha:0.5];
                     } 
                     completion:^(BOOL finished) {
                         [UIView animateWithDuration:0.1
                                               delay:0.0
                                             options:0
                                          animations:^{
                                              [self setFrame:CGRectMake(20, 120, 280, 170)];
                                          } 
                                          completion:nil];
                     }];
}

-(void)dealloc
{
    self.delegate=nil;
    [super dealloc];
}
@end
